freeslot("MT_GOHLA", "S_GOHLA_STND", "S_GOHLA_RUN", "MT_GOHLA_BALL", "S_GOHLA_BALL", "S_GOHLA_BALL2", "SPR_GOLA")
// Remind Glaber to update Badnik Pack DX with the updated code for Gola

local function CustomFlameySpawn(mobj, mapthing)
    local offset
    
    if mapthing.z != 0
        offset = mapthing.z
    else
        offset = 12*FRACUNIT
    end
    
    if (mapthing.options & MTF_OBJECTFLIP)
        if offset != 0
            mobj.z = mobj.z - offset
        else
            mobj.z = mobj.ceilingz
        end
    else
        if offset != 0
            mobj.z = mobj.z + offset
        else
            mobj.z = mobj.floorz
        end
    end
    
end

addHook("MapThingSpawn", CustomFlameySpawn, MT_GOHLA)

//Gohla ball spawn code
--Code by Midiman
addHook("MobjSpawn", function(mo)
    if not (mo and mo.valid) then return end

	-- Spawn "damage" number of "painchance" flameball mobjs
	-- threshold is the distance they should keep from the MT_GOHLA (touching radius + ball painchance)
	for i = 0, mo.info.damage - 1 do
	    local ball = P_SpawnMobjFromMobj(mo, 0, 0, 0, mo.info.painchance)
	    if ball and ball.valid then
		   ball.target = mo
		   ball.movedir = FixedAngle(FixedMul(FixedDiv(i*FRACUNIT, mo.info.damage*FRACUNIT), 360*FRACUNIT))
		   ball.threshold = ball.radius + mo.radius + FixedMul(ball.info.painchance, ball.scale)

		   local var1, var2 = states[ball.state].var1, states[ball.state].var2
		   states[ball.state].action(ball, var1, var2)
		end
	end
end, MT_GOHLA)


// 2.1 style Gohla
mobjinfo[MT_GOHLA] = {
		//$Name "Gohla"
		//$Sprite GOLAA1
		//$Category Genesis Enemies
        doomednum = 3000,
        spawnstate = S_GOHLA_STND,
        spawnhealth = 1,
        seestate = S_GOHLA_RUN,
        seesound = sfx_None,
        reactiontime = 32,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = MT_GOHLA_BALL,
        painsound = sfx_None,
		missilestate = S_NULL,
        deathstate = S_XPLD_FLICKY,
        xdeathstate = S_NULL,
        deathsound = sfx_pop,
        speed = 2,
        radius = 18*FRACUNIT,
        height = 36*FRACUNIT,
        dispoffset = 0,
        mass = 4*FRACUNIT,
        damage = 5,
        activesound = sfx_None,
		raisestate = S_NULL,
        flags = MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE|MF_NOGRAVITY
}
	
states[S_GOHLA_STND] = {SPR_GOLA, A, 4, A_Look, (1058<<1), 0, S_GOHLA_STND} -- The range limit doesn't appear to work in the later part of Azure Peaks
states[S_GOHLA_RUN] = {SPR_GOLA, A, 1, A_Chase, 0, 0, S_GOHLA_RUN}

// GOHLA Fireball, code modified from Midiman's 2.1 eggmobile spike ball code.

mobjinfo[MT_GOHLA_BALL] = {
	doomednum = -1,
	spawnstate = S_GOHLA_BALL,
	spawnhealth = 1,
	seesound = sfx_None,
	reactiontime = 1,
	painchance = 20*FRACUNIT,
	speed = 2*FRACUNIT,
	radius = 13*FRACUNIT,
	height = 26*FRACUNIT,
	damage = 8*FRACUNIT,
	mass = DMG_FIRE,
	flags = MF_PAIN|MF_NOGRAVITY|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOCLIPTHING
}

// Replace A_GOHLABall with a 2.1 variant, if it changes in the future
states[S_GOHLA_BALL] = {SPR_GOLA, FF_FULLBRIGHT|FF_TRANS40|B, 1, A_UnidusBall, 1, 0, S_GOHLA_BALL2}
states[S_GOHLA_BALL2] = {SPR_GOLA, FF_FULLBRIGHT|FF_TRANS40|C, 1, A_UnidusBall, 1, 0, S_GOHLA_BALL}

--Puyo
if not(kirbyabilitytable) then
    rawset(_G, "kirbyabilitytable", {})
end
kirbyabilitytable[MT_GOHLA] = 1    -- Makes MT_GOHLA give Fireball